package com.csoa.community.activity;

import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.ThumbnailUtils;
import android.net.Uri;
import android.provider.MediaStore.Images;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.collect.community.R;
import com.csoa.community.adapter.WZMLTableAdapter;
import com.csoa.community.db.WZMLInfoDB;
import com.csoa.community.utils.AndroidUtils;
import com.csoa.community.utils.StorageCheckor;
import com.csoa.community.utils.StringUtils;
import com.csoa.community.widget.CommonDialog;
import com.csoa.community.widget.CommonDialog.OnListItemClickListener;

@SuppressLint("NewApi")
@TargetApi(8)
public class BaseProcessPhotoActivity extends BaseActivity {

	public List<ViewHolder> imgViewHolders = new ArrayList<BaseProcessPhotoActivity.ViewHolder>();
	public List<ViewHolder> VedioViewHolders = new ArrayList<BaseProcessPhotoActivity.ViewHolder>();
	public StringBuffer sbImgName = new StringBuffer("");
	public StringBuffer sbImgUrl = new StringBuffer("");
	public StringBuffer sbVideoUrl = new StringBuffer("");
	public StringBuffer sbVideoName = new StringBuffer("");
	public String imgTitle = "";
	public String imgAuthor = "";
	public String videoTitle;
	public String videoAuthor;
	public LinearLayout layoutVedioYF;
	public LinearLayout layoutCameraYF;
	public int mWhich;

	/**
	 * 保存图片
	 * 
	 * @return
	 */
	public String showImg(LinearLayout layout, Intent data) {
		boolean isExist = StorageCheckor.checkSdcard(this);
		if (isExist) {
			Uri uriVideo = data.getData();
			String strVideoPath = "";
			Cursor cursor = this.getContentResolver().query(uriVideo, null,
					null, null, null);
			if (cursor.moveToNext()) {
				strVideoPath = cursor.getString(cursor.getColumnIndex("_data"));
			}
			setImageBitmap(strVideoPath, layout);
			return strVideoPath;
		}
		return null;
	}

	public boolean isCamera = true;

	public static class ViewHolder {
		ImageView image;
		EditText etCameraName;
	}

	public void fillBitmapToLayout(String imgPath, ViewGroup layout,
			boolean isCamera) {
		this.isCamera = isCamera;
		fillBitmapToLayout(imgPath, layout);
	}

	public void fillBitmapToLayout(String imgPath, String imgName,
			ViewGroup layout, boolean isCamera) {
		this.isCamera = isCamera;
		fillBitmapToLayout(imgPath, imgName, layout);
	}

	public void fillBitmapToLayout(String imgPath, String imgName,
			ViewGroup layout) {
		String[] imgPaths = imgPath.split(",");
		String[] imgNames = imgName.split(",");
		layout.removeAllViews();
		if (isCamera) {
			imgViewHolders.clear();
		} else {
			VedioViewHolders.clear();
		}
		for (int i = 0; i < imgPaths.length; i++) {
			setImageBitmap(imgPaths[i], imgNames[i], layout);
		}
		if (isCamera) {
			layout.setTag(imgViewHolders);
		} else {
			layout.setTag(VedioViewHolders);
		}

	}

	/**
	 * show img
	 * 
	 * @param imgPath
	 */
	public void fillBitmapToLayout(String imgPath, ViewGroup layout) {
		String[] imgPaths = imgPath.split(",");
		layout.removeAllViews();
		for (int i = 0; i < imgPaths.length; i++) {
			setImageBitmap(imgPaths[i], layout);
		}
		layout.setTag(imgViewHolders);
	}

	public void setImageBitmap(final String path, ViewGroup layout) {
		setImageBitmap(path, subPhotoNameByPath(path), layout);
	}

	public void setImageBitmap(final String path, final String name,
			final ViewGroup layout) {
		Bitmap smallBitmap = getBitmap(path, isCamera);
		if (smallBitmap != null) {
			final ViewHolder viewHolder = new ViewHolder();
			final LinearLayout layoutCaramer = (LinearLayout) LayoutInflater
					.from(this).inflate(R.layout.plug_camera, null);
			viewHolder.image = (ImageView) layoutCaramer
					.findViewById(R.id.camera);
			viewHolder.image.setImageBitmap(smallBitmap);
			viewHolder.image.setTag(path);
			viewHolder.image.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View v) {
					if(mWhich == 0) {
						AndroidUtils.fileInvoke(_activity, path);
					}else {
						showDefineDialog(path, viewHolder, layout, layoutCaramer);
					}
				}
			});

			viewHolder.etCameraName = (EditText) layoutCaramer
					.findViewById(R.id.cameraTitle);
			if(mWhich == 0) {
				viewHolder.etCameraName.setBackgroundResource(0);
				viewHolder.etCameraName.setEnabled(false);
			}
			String carmeraName = name;
			viewHolder.etCameraName.setText(carmeraName);
			if (isCamera) {
				imgViewHolders.add(viewHolder);
			} else {
				VedioViewHolders.add(viewHolder);
			}
			layout.addView(layoutCaramer);
		}
	}

	public String subPhotoNameByPath(String path) {
		if (!StringUtils.isEmpty(path)) {
			return path.substring(path.lastIndexOf("/") + 1, path.length());
		}
		return "";
	}

	/**
	 * 获取拍照或摄像缩略图的bitmap
	 * 
	 * @param path
	 * @param isCamera拍照传true
	 *            ，摄像传false
	 * @return
	 */
	private Bitmap getBitmap(String path, boolean isCamera) {
		Bitmap bitmap = null;
		if (isCamera) {
			bitmap = BitmapFactory.decodeFile(path,
					AndroidUtils.getBitmapOptions(path));
		} else {
			Bitmap sourceBitmap = ThumbnailUtils.createVideoThumbnail(path,
					Images.Thumbnails.MINI_KIND);
			bitmap = ThumbnailUtils.extractThumbnail(sourceBitmap, 120, 90,
					ThumbnailUtils.OPTIONS_RECYCLE_INPUT);
		}
		return bitmap;
	}

	/**
	 * 重新从EditText中获取图片名称 获取所有图片的名称 图片名称以逗号风格
	 * 
	 * @return
	 */
	public String getImgName(boolean argisCamera) {
		String imgName = "";
		if (argisCamera) {
			sbImgName = new StringBuffer("");
			if (imgViewHolders != null) {
				for (int i = 0; i < imgViewHolders.size(); i++) {
					EditText etName = imgViewHolders.get(i).etCameraName;
					sbImgName.append(etName.getText().toString() + ",");
				}
				imgName = AndroidUtils.replaceTheEndComma(sbImgName.toString());
			}
		} else {
			sbVideoName = new StringBuffer("");
			if (VedioViewHolders != null) {
				for (int i = 0; i < VedioViewHolders.size(); i++) {
					EditText etName = VedioViewHolders.get(i).etCameraName;
					sbVideoName.append(etName.getText().toString() + ",");
				}
				imgName = AndroidUtils.replaceTheEndComma(sbVideoName
						.toString());
			}
		}

		return imgName;
	}

	public String getImgPath(boolean argisCamera) {
		String imgPath = "";
		if (argisCamera) {
			sbImgName = new StringBuffer("");
			if (imgViewHolders != null) {
				for (int i = 0; i < imgViewHolders.size(); i++) {
					ImageView imgView = imgViewHolders.get(i).image;
					sbImgName.append(imgView.getTag() + ",");
				}
				imgPath = AndroidUtils.replaceTheEndComma(sbImgName.toString());
			}
		} else {
			sbVideoName = new StringBuffer("");
			if (VedioViewHolders != null) {
				for (int i = 0; i < VedioViewHolders.size(); i++) {
					ImageView imgView = VedioViewHolders.get(i).image;
					sbVideoName.append(imgView.getTag() + ",");
				}
				imgPath = AndroidUtils.replaceTheEndComma(sbVideoName
						.toString());
			}
		}

		return imgPath;
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode == RESULT_OK) {
			if (requestCode == 1) {
				sbImgUrl = new StringBuffer("");
				sbImgName = new StringBuffer("");
				String imgPath = data.getStringExtra("imgPath");
				String imgName = data.getStringExtra("imgName");
				imgTitle = data.getStringExtra("title");
				imgAuthor = data.getStringExtra("author");
				if (!StringUtils.isEmpty(imgPath)) {
					sbImgUrl.append(imgPath);
					sbImgName.append(imgName);
					fillBitmapToLayout(sbImgUrl.toString(),
							sbImgName.toString(), layoutCameraYF, true);
				}
			} else if (requestCode == 2) {
				sbVideoUrl = new StringBuffer("");
				sbVideoName = new StringBuffer("");
				String imgPath = data.getStringExtra("imgPath");
				String imgName = data.getStringExtra("imgName");
				videoTitle = data.getStringExtra("title");
				videoAuthor = data.getStringExtra("author");
				if (!StringUtils.isEmpty(imgPath)) {
					sbVideoUrl.append(imgPath);
					sbVideoName.append(imgName);
					fillBitmapToLayout(sbVideoUrl.toString(),
							sbVideoName.toString(), layoutVedioYF, false);
				}
			}

		}
	}

	private void showDefineDialog(final String path,
			final ViewHolder viewHolder, final ViewGroup layout,
			final LinearLayout layoutCaramer) {
		String items[] = new String[] { "查看", "删除" };
		CommonDialog caculatorDialog = new CommonDialog(this);
		caculatorDialog.setItems(items, new OnListItemClickListener() {

			@Override
			public void onListItemClick(int which) {
				if (which == 0) {
					AndroidUtils.fileInvoke(_activity, path);
				}
				if (which == 1) {
					layout.removeView(layoutCaramer);
					imgViewHolders.remove(viewHolder);
					VedioViewHolders.remove(viewHolder);
				}

			}
		});
		caculatorDialog.show();
	}

}
